package com.android.geotrack.artist;

import java.util.LinkedList;

import com.android.geotrack.R;
import com.android.geotrack.R.id;
import com.android.geotrack.R.layout;

import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;

public class ArtistsActivity extends Activity
{
	private ListView listView;
	private ProgressDialog pd;
	private String user;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		Bundle b = this.getIntent().getExtras();
		this.user = b.getString("user");
		setContentView(R.layout.related_artists);
		listView = (ListView) findViewById(R.id.related_artists_list);
		Log.v("Activity creation", "ArtistsActivity");
		
		// back button
		final Button backButton = (Button) findViewById(R.id.artistsBackButton);
		backButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {            	
            	finish();
            }
        });
	}
	
	@Override
	protected void onResume()
	{
		super.onResume();
		if(!LoadArtistsRunnable.lar_started) {
			loadArtists = new Thread(new LoadArtistsRunnable(this, user));
			pd = ProgressDialog.show(this, "Working..", "Loading related artists", true, false);
			loadArtists.start();
		}
	}

	private Thread loadArtists;
		
	public Handler messageHandler = new Handler() 
		{
			@Override
			public void handleMessage(Message msg)
			{
				pd.dismiss();
				
				switch(msg.what) {
				case 42:
					Object[] result = (Object[]) msg.obj;
					LinkedList<Artist> artists = (LinkedList<Artist>) result[0];
					LinkedList<Drawable> thumbs = (LinkedList<Drawable>) result[1];
					
					// generate list of artist names (temporary?)
					LinkedList<String> artist_strings = new LinkedList<String>();
					for(Artist a : artists)
						artist_strings.add(a.getName());
						
					// set list adapter
					ArtistsActivity.this.listView.setAdapter(new ArtistAdapter(ArtistsActivity.this, artist_strings, thumbs));
					break;
				}
			}
		};

}